#!/bin/awk -f
## select tiling(s) number from tilings.out using index file *erep
## Synopsis: cat TILING | poperep EREPFILE
BEGIN{
  fn=ARGV[1]; sel=0; na=0;
  while(getline<fn)if($0!~/^ *\#/){
    s[$1]++;
    x=$0; sub(/^.*\#/,"",x); split(x,a);
    if($1>sel){sel=$1};
    na++; e[$1]=a[2]; nc[$1]=a[1];
  }close(fn);
  printf "" > "pop.erep";
  ARGV[1]="-"; 
  ARGC=2;
  k=0;
}
FNR==1{
  nn=$1; mu=$2; nt=$3;
  print nn,mu,na"  # tilings via poptils";
}
FNR>1&&FNR<4{print} #basis
FNR==3{
  for(i=1;i<=sel;i++){
    pr=0; if(s[i]>0){
      k++;
      print k,e[i],nc[i]>>"pop.erep";
      pr=1;
    };
    if(sel>nt){exit}
    for(j=1;j<=nn;j++){
      getline; 
      if(pr>0){print $0}
    }
  }
}
